package com.gy.zpchat.ui.chat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import im.zego.zim.callback.ZIMCombineMessageDetailQueriedCallback;
import im.zego.zim.entity.ZIMCombineMessage;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.enums.ZIMErrorCode;
import im.zego.zim.enums.ZIMMessageType;
import com.gy.zpchat.R;
import com.gy.zpchat.SDKManager;
import com.gy.zpchat.entity.MessageInfo;
import com.gy.zpchat.ui.chat.base.adapter.CombineMessageAdapter;
import com.gy.zpchat.ui.chat.base.adapter.OnItemClickListener;

public class CombineMessageActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private CombineMessageAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_combine_message);

        adapter = new CombineMessageAdapter(this);
        setClick();
        recyclerView = findViewById(R.id.rv_message_list);
        ZIMCombineMessage message = (ZIMCombineMessage) getIntent().getSerializableExtra("message");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        SDKManager.sharedInstance().queryCombineMessageDetail(message, new ZIMCombineMessageDetailQueriedCallback() {
            @Override
            public void onCombineMessageDetailQueried(ZIMCombineMessage combineMessage, ZIMError error) {
                if (error.code == ZIMErrorCode.SUCCESS){
                    List<MessageInfo> messageInfos = Objects.requireNonNull(combineMessage.messageList).stream().map(message -> {
                        MessageInfo info = new MessageInfo();
                        info.setZIMMessage(message);
                        return info;
                    }).collect(Collectors.toList());
                    adapter.setMessageList(new ArrayList<>(messageInfos));
                    adapter.notifyItemRangeInserted(0,adapter.getItemCount());
                }
            }
        });
    }

    private void setClick(){
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemReactionClick(String emoji, MessageInfo messageInfo) {

            }

            @Override
            public void onItemLongClick(MessageInfo messageInfo) {

            }

            @Override
            public void onItemSelectCheck(boolean isCheck, MessageInfo messageInfo) {

            }

            @Override
            public void onItemClick(MessageInfo messageInfo) {
                if (messageInfo.getZIMMessage().getType() == ZIMMessageType.COMBINE){
                    ZIMCombineMessage message = (ZIMCombineMessage) messageInfo.getZIMMessage();
                    CombineMessageActivity.actionStart(CombineMessageActivity.this,message);
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        SDKManager.sharedInstance().showDebugLog(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        SDKManager.sharedInstance().hideDebugLog(this);
    }

    public static void actionStart( Context context, ZIMCombineMessage message){
        Intent intent = new Intent(context, CombineMessageActivity.class);
        intent.putExtra("message",message);
        context.startActivity(intent);
    }
}